package com.wakala.employment.fragment.news;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.faltenreich.skeletonlayout.Skeleton;
import com.faltenreich.skeletonlayout.SkeletonLayoutUtils;
import com.kongzue.dialogx.dialogs.CustomDialog;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.leaf.library.StatusBarUtil;
import com.wakala.employment.R;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.core.http.subscriber.NoTipRequestSubscriber;
import com.wakala.employment.databinding.FragmentNewDetailBinding;
import com.wakala.employment.http.TestApi;
import com.wakala.employment.http.bean.ArticlePage;
import com.wakala.employment.http.bean.ArticleVo;
import com.xuexiang.xhttp2.XHttpProxy;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xpage.utils.GsonUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.imageview.preview.MediaLoader;
import com.xuexiang.xui.widget.imageview.preview.loader.ISimpleTarget;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.tip.ToastUtils;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.SimpleImageFixCallback;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewDetailFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class NewDetailFragment extends BaseFragment<FragmentNewDetailBinding> implements View.OnClickListener {

    private static String TAG=NewDetailFragment.class.getSimpleName();
    private CustomDialog customDialog = null;//搜索市等待对话框

    /**
     * 文章ID
     */
    private Long  articleId;

    @NonNull
    @Override
    protected FragmentNewDetailBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentNewDetailBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        StatusBarUtil.setDarkMode(getActivity());
        //设置状态栏背景颜色，如果设置为 Color.TRANSPARENT 将会隐藏状态栏
        getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        return null;
    }


    @Override
    protected void initArgs() {
        assert getArguments() != null;
        articleId = getArguments().getLong("articleId");
    }

    @Override
    protected void initViews() {
        binding.contentRoot.setVisibility(View.GONE);
        initCustomDialog();
        //加载文章数据
        getArticleInfo();

    }
    private void initCustomDialog(){
        customDialog = CustomDialog.build()
                .setCustomView(new OnBindView<CustomDialog>(R.layout.dialog_layout_spinkit) {
                    @Override
                    public void onBind(final CustomDialog dialog, View v) {
                      /*  ImageView btnOk;
                        btnOk = v.findViewById(R.id.btn_ok);
                        btnOk.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });*/
                    }
                });
    }

    @Override
    protected void initListeners() {
       binding.tvBack.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.tv_back){
            popToBack();
        }
    }

    private void bingData(ArticleVo articleVo){
        if(null==articleVo) return;
        //标题
        if(StringUtils.isEmpty(articleVo.getTitle())){
            binding.tvTitle.setVisibility(View.GONE);
        }else{
            binding.tvTitle.setText(articleVo.getTitle());
        }
        //图片
        if(StringUtils.isEmpty(articleVo.getImageInput())){
            binding.ivImage.setVisibility(View.GONE);
        }else {
            MediaLoader.get().displayImage(this, articleVo.getImageInput() ,binding.ivImage, new ISimpleTarget(){
                @Override
                public void onResourceReady() {

                }

                @Override
                public void onLoadFailed(@Nullable Drawable drawable) {
                    if (drawable != null) {
                        binding.ivImage.setImageDrawable(drawable);
                    }
                }
            });
        }
        //内容
        RichText.fromHtml(articleVo.getContent()).autoPlay(true).singleLoad(false).fix(new SimpleImageFixCallback() {
            @Override
            public void onInit(ImageHolder holder) {
                if (holder.isGif()) {
                    holder.setAutoFix(false);
                }
                holder.setAutoPlay(true);
                holder.setAutoFix(true);
            }
        }).into(binding.tvContent);
    }


    @SuppressLint("CheckResult")
    private void getArticleInfo() {
        customDialog.show();
        XHttpProxy.proxy(TestApi.IArticleService.class)
                .getArticleInfo(articleId)
                .subscribeWith(new NoTipRequestSubscriber<ArticleVo>() {
                    @SuppressLint("CheckResult")
                    @Override
                    protected void onSuccess(ArticleVo articleVo) {
                        Log.i(TAG, "-------------文章西悉尼------------------");
                        Log.i(TAG, GsonUtils.toJson(articleVo));
                        bingData(articleVo);
                        binding.contentRoot.setVisibility(View.VISIBLE);
                        customDialog.dismiss();
                        Log.i(TAG, "-------------文章西悉尼------------------");
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        ToastUtils.toast(e.getDisplayMessage());
                        customDialog.dismiss();
                        Log.i(TAG, "-------------文章西悉尼-返回错误------------------");
                        Log.e(TAG, e.getMessage());
                    }
                });

    }

}